package com.wss.module.market.ui.goods.cart;

import com.orhanobut.logger.Logger;
import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.bean.Event;
import com.wss.common.constants.Dic;
import com.wss.common.constants.EventAction;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ColorUtils;
import com.wss.common.utils.ImageUtils;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.TextUtils;
import com.wss.module.market.ResourceTable;
import com.wss.module.market.bean.GoodsInfo;
import com.wss.module.market.bean.Vendor;
import com.wss.module.market.ui.goods.cart.adapter.ShoppingCartAdapter;
import com.wss.module.market.ui.goods.cart.mvp.CartPresenter;
import com.wss.module.market.ui.goods.cart.mvp.contract.ShoppingCartContract;
import com.wss.module.market.utils.ShoppingCartUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartAbility extends BaseActionBarAbility<CartPresenter>
        implements ShoppingCartContract.View,
        Component.ClickedListener {

    ListContainer recycleView;

    Image ivCheckAll;

    Text tvTotal;

    Text btnBuy;

    private Text tvRight;
    private boolean isEdit;

    private List<Vendor> mData = new ArrayList<>();
    private ShoppingCartAdapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        findView();
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_ability_shopping_cart;
    }

    @Override
    protected CartPresenter createPresenter() {
        return new CartPresenter();
    }

    @Override
    public void refreshCartData(List<Vendor> dataList) {

        this.mData.clear();
        this.mData.addAll(dataList);
        adapter.clear();
        adapter.addAll(dataList);
        displayResult();
    }

    protected void findView() {
        recycleView = (ListContainer) findComponentById(ResourceTable.Id_recycle_view);
        ivCheckAll = (Image) findComponentById(ResourceTable.Id_iv_check_all);
        tvTotal = (Text) findComponentById(ResourceTable.Id_tv_total);
        btnBuy = (Text) findComponentById(ResourceTable.Id_btn_buy);

        ivCheckAll.setClickedListener(this::onClick);
        btnBuy.setClickedListener(this::onClick);
    }

    protected void initView() {
        setCenterText("购物车");
        adapter = new ShoppingCartAdapter(context, mData, (data, position) -> {

        });

        recycleView.setItemProvider(adapter);
        tvRight = getTitleBar().getTextView();
        tvRight.setText("编辑");
        tvRight.setClickedListener(v -> onRightChange());
        getTitleBar().setRightView(tvRight);
        getPresenter().start();
    }

    private void onRightChange() {
        isEdit = !isEdit;
        if (isEdit) {
            tvRight.setText("完成");
            tvTotal.setVisibility(Component.HIDE);
            btnBuy.setText("删除");
            btnBuy.setBackground(ColorUtils.colorResToElement(this, ResourceTable.Color_red));
        } else {
            tvTotal.setVisibility(Component.VISIBLE);
            tvRight.setText("编辑");
            btnBuy.setText("下一步");
            if (btnBuy.isSelected()) {
                btnBuy.setBackground(ColorUtils
                        .colorResToElement(this,ResourceTable.Color_orange));
            } else {
                btnBuy.setBackground(ColorUtils
                        .colorResToElement(this,ResourceTable.Color_gray));
            }
        }
        adapter.setEdit(isEdit);
    }

    @Override
    protected boolean registerEventBus() {
        return true;
    }

    @Override
    public void onEventBus(Event event) {
        super.onEventBus(event);
        if (TextUtils.equals(EventAction.EVENT_SHOPPING_CART_CHANGED, event.getAction())) {
            //购物车有变化
            adapter.notifyDataChanged();
            ivCheckAll.setSelected(ShoppingCartUtils.isAllVendorChecked(mData));
            if (ivCheckAll.isSelected()) {
                ivCheckAll.setPixelMap(ResourceTable.Media_market_icon_checkbox_checked);
            } else {
                ivCheckAll.setPixelMap(ResourceTable.Media_market_icon_checkbox_default);
            }
            displayResult();

        } else if (TextUtils.equals(EventAction.EVENT_SHOPPING_CART_REFRESH, event.getAction())) {
            //重新获取购物车数据
            getPresenter().getCartData();
        }
    }

    private void displayResult() {
        btnBuy.setSelected(ShoppingCartUtils.isCheckedLeastOne(mData));
        if (btnBuy.isSelected()) {
            btnBuy.setBackground(ColorUtils
                    .colorResToElement(this,ResourceTable.Color_orange));
        } else {
            btnBuy.setBackground(ColorUtils
                    .colorResToElement(this,ResourceTable.Color_gray));
        }
        tvTotal.setText(String.
                format("%s%s", getString(ResourceTable.String_market_total),
                        ShoppingCartUtils.getCartCountPrice(mData)));
    }

    @Override
    public void onEmpty(Object tag) {
        super.onEmpty(tag);
        showEmptyView("车里空空如也");
    }

    @Override
    public void onClick(Component view) {
        int i = view.getId();
        if (i == ResourceTable.Id_iv_check_all) {
            boolean selected = view.isSelected();
            view.setSelected(!selected);
            if (view.isSelected()) {
                ((Image)view).setPixelMap(ResourceTable.Media_market_icon_checkbox_checked);

            } else {
                ((Image)view).setPixelMap(ResourceTable.Media_market_icon_checkbox_default);
            }

            ShoppingCartUtils.checkAll(mData, !selected);
            adapter.notifyDataChanged();
            displayResult();
        } else if (i == ResourceTable.Id_btn_buy) {
            List<GoodsInfo> checkedGoods = ShoppingCartUtils.getAllCheckedGoods(mData);
            if (checkedGoods.size() > 0) {
                if (isEdit) {
                    //删除
                    ShoppingCartUtils.delete(checkedGoods);
                } else {
                    Map<String, Object> param = new HashMap<>();
                    param.put(Dic.VENDOR_GOODS_INFO, JsonUtils.toJson(ShoppingCartUtils.getCheckedGoodsVendor(mData)));
                    AbilityToAbility.toAbility(context, OrderSettlementAbility.class, param);
                }
            }
        }
    }
}
